<?php
$link = mysql_connect('localhost', 'root', '');
//$link = mysql_connect('aecabill.accountsupportmysql.com', 'aeca', 'fArC5YyyG8NT9wdC5LXP');
if (!$link) { 
     die('There appeared to be an error while connecting to the website database: ' . mysql_error()); 
}
mysql_select_db(aeca);

$p_id = $_POST['bill_id'];
$bill_query = "select tbl_period.*,tbl_period_split.* from tbl_period inner join tbl_period_split on tbl_period.p_end_date = tbl_period_split.p_end_date_trunk OR tbl_period.p_end_date = tbl_period_split.p_end_date_no_trunk where p_id=$p_id";

if(mysql_num_rows(mysql_query($bill_query)) < 1){
	$bill=mysql_query("select * from tbl_period where p_id = $p_id");
	$result = mysql_fetch_array($bill);
}
else{
	$splitbill = mysql_query($bill_query);
	$result = mysql_fetch_array($splitbill);
}
?>
<script>
 $(function() {  
   $(".editbill").click(function() { 
     	// validate and process form here
     	var name = 'edit bill';
     	var id = $("#pid").val();
    	var p_start_date = $(".p_start_date").val();
		var p_end_date = $(".p_end_date").val();
		var p_split_trunk_value = $(".p_split:checked").val();
	    $.post("/aec1/pages/update_cprofile.php",{name:name,p_start_date:p_start_date,p_end_date:p_end_date,p_split_trunk_value:p_split_trunk_value,id:id},function(data){
     	if(data == 1)
     	{
     		$("#error").text("Billing Period End Date Already Exists!").show();
     	}else{
     		$( "#dialog:ui-dialog" ).dialog( "destroy" );
			$( "#dialog-message_u" ).dialog({
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog( "close" );
					window.location = "index.php?page=report-mang";
				}
			}
			});
     	}
	     });
	     return false;
   });  
 });
</script>
<script>
$(document).ready(function(){
if($('.split_trunk:checked').val() == 'split_trunk'){
	$(".split").slideDown('fast');
}
//showup the checked split billing
$(".split_trunk").click(function () {
if ($('.split_trunk:checked').val() !== undefined) {
$(".split").slideDown('fast');
} else {
$(".split").slideUp('fast');
}
}); 
});
</script>
<script>
$(function() {
var dates = $( "#from, #to" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 1,
			onSelect: function( selectedDate ) {
				var option = this.id == "from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" );
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
});
</script>
<script>
	$(function() {
		var dates = $( "#from2, #to2" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 1,
			onSelect: function( selectedDate ) {
				var option = this.id == "from2" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" );
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
	});
</script>
<!-- ui-dialog -->
<div id="dialog-message_u" title="Success Update" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		The Billing Period has been Updated Successfully.
	</p>
</div>
<span id='error' style="font-size:11px;"></span><span id='success' style="font-size:11px;"></span>
<form action="" method="post" id-"billfrm">
	<div class="billing_period">
		<div style="padding:5px;">
			<span>Start Date: </span>
			<span><input type="text" name="p_start_date" id="from" class="required p_start_date" value="<?=$result['p_start_date'];?>"/></span>
		</div>
		<div style="padding:5px;">
			<span style="padding-right:9px;">End Date: </span>
			<span><input type="text" name="p_end_date" id="to" class="required p_end_date" value="<?=$result['p_end_date'];?>" readonly/></span>
		</div>
		<div style="padding:5px;">
			<span><strong>Split Billing Period</strong></span>
			<span><input type="checkbox" class="split_trunk" name="split_trunk" value="split_trunk" <?php if(mysql_num_rows(mysql_query($bill_query)) > 0){echo 'checked';}?> /></span>
		</div>
		<div style="display:none;" class="split">
			<div style="padding:5px;">
				<span>p_split_no_trunk </span>
				<span><input type="radio" class="p_split" name="p_split" value="p_split_no_trunk" <?php if($result['p_split_no_trunk'] == 'True'){echo 'checked';}?> /></span>
			</div>
			<div style="padding:5px;">
				<span style="padding-right:18px;">p_split_trunk </span>
				<span><input type="radio" class="p_split" name="p_split" value="p_split_trunk" <?php if($result['p_split_trunk'] == 'True'){echo 'checked';}?> /></span>
			</div>
		</div>
		<input type="hidden" name="periodid" value="<?=$result['p_id'];?>" id="pid"/>
	<input type="submit" value="submit" name="submit" class="editbill"/>
	<input type="button" value="Cancel" name="cancel" onclick="location.href='index.php?page=report-mang'"/>
	</div>
</form>